package com.hsxshop.controller;

import com.hsxshop.model.Pagination;
import com.hsxshop.model.OSupply;
import com.hsxshop.model.User;
import com.hsxshop.service.BaseEntityService;
import com.hsxshop.service.OSupplyService;
import com.hsxshop.service.UserService;
import com.hsxshop.util.*;
import lombok.extern.java.Log;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangchongya on 17/7/17.
 */
@RestController
@RequestMapping(value = "/osupply/")
@Log
public class OSupplyController {

    @Resource(name = "osupplyService")
    private OSupplyService osupplyService;

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "baseEntityService")
    private BaseEntityService baseEntityService;

    @RequestMapping(value = "list", method = RequestMethod.POST,produces= MediaType.APPLICATION_JSON_UTF8_VALUE,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String index(@RequestBody Map requestMap  ){
        Pagination pagination= PaginationTool.getPagination(requestMap);


        String phone=ParamConvertTool.getParamStr(requestMap,"phone");
        String beginDisplayTime=ParamConvertTool.getParamStr(requestMap,"beginDisplayTime");
        String endDisplayTime=ParamConvertTool.getParamStr(requestMap,"endDisplayTime");

        String beginArrivalTime=ParamConvertTool.getParamStr(requestMap,"beginArrivalTime");
        String endArrivalTime=ParamConvertTool.getParamStr(requestMap,"endArrivalTime");

        String whereStr="";
        if(null!=requestMap.get("state")){
            int state=ParamConvertTool.getParamInt(requestMap,"state");
            if(state!=-2) {
                whereStr = " and state=" + state;
            }
        }
        if(!"".equals(phone)){
            User whereU= userService.getEntity(" from User where phone=?0",phone);
            if(null!=whereU){
                whereStr=" and userId="+whereU.getId();
            }else{
                return JsonTool.returnObjectJsonErrorMsg("该手机不存在");
            }
        }
        if(!"".equals(beginDisplayTime)){
            whereStr=" and displayTime>"+beginDisplayTime;
        }
        if(!"".equals(endDisplayTime)){
            whereStr=" and displayTime<"+endDisplayTime;
        }
        if(!"".equals(beginArrivalTime)){
            whereStr=" and arrivalTime>"+beginArrivalTime;
        }
        if(!"".equals(endArrivalTime)){
            whereStr=" and arrivalTime<"+endArrivalTime;
        }
        List<OSupply> list=osupplyService.findByHQLWithPagination(pagination, SqlTool.returnSql("new OSupply( id,serialNum, userId, title, fishName, fishInfo, quantity, unit, unitWeight, price, tradePlace, townId, createTime, updateTime, displayTime, arrivalTime, state, generateOrder, displayType, remark,images)", "OSupply", " where displayTime<?0"+whereStr, "order by displayTime desc"), DateTool.getStringDate());
        //获取当前时间
        List returnLists=new ArrayList();
        for (OSupply oSupply:list) {
            Map map=new HashMap();
            map.put("id",oSupply.getId());

            //查找用户手机
            String user_phone="";
            User u=userService.getEntity(oSupply.getUserId());
            String name="".equals(u.getName()) ? u.getNickname() : u.getName();
            if("".equals(name)){
                user_phone=u.getPhone();
            }else{
                user_phone=u.getPhone()+"("+name+")";
            }
            map.put("user_phone",user_phone);
            map.put("title",oSupply.getTitle());
            map.put("fishInfo",oSupply.getFishInfo());
            map.put("quantity",oSupply.getQuantity()+oSupply.getUnit());
            map.put("priceInfo",oSupply.getPrice()/100.0+"元/"+oSupply.getUnit());
            map.put("displayTime",oSupply.getDisplayTime());
            map.put("state",oSupply.getState());
            returnLists.add(map);
        }
        return JsonTool.returnListJsonSuccess(returnLists,pagination);
    }

    @RequestMapping(value = "detail", method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String detail(@RequestBody Map requestMap){

        Integer id=(Integer)requestMap.get("id");
        OSupply oSupply=osupplyService.getEntity(id);
        if(null==oSupply){
            return JsonTool.returnObjectJsonErrorMsg("该供应不存在");
        }
        Map map=new HashMap();
        //查找用户手机
        User u=userService.getEntity(oSupply.getUserId());
        map.put("user_phone",ParamConvertTool.getUserPhone(u));
        map.put("id",oSupply.getId());
        map.put("title",oSupply.getTitle());
        map.put("fishInfo",oSupply.getFishInfo());
        map.put("quantity",oSupply.getQuantity()+oSupply.getUnit());
        map.put("priceInfo",oSupply.getPrice()/100.0+"元/"+oSupply.getUnit());
        map.put("displayTime",oSupply.getDisplayTime());
        map.put("arrivalTime",oSupply.getArrivalTime());
        map.put("state",oSupply.getState());
        map.put("remark",oSupply.getRemark());
        map.put("images",oSupply.getImages());
        map.put("tradePlace",oSupply.getTradePlace());
        //获取属性值
        String sql="select nav.storage,nav.attrValue,na.name,na.sort from nhsx_attribute_value nav inner join nhsx_attribute na on na.id=nav.attrId where nav.pid=?0 and na.storage=?1 order by na.sort desc";
        List<Map> attributeMaps=baseEntityService.getListMapByHQL(sql,oSupply.getId(),0);
        map.put("attriButeStr",AttributeTool.getAttributeStr(attributeMaps));
        return JsonTool.returnObjectJsonSuccess(map);
    }

    @RequestMapping(value = "edit", method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,consumes =MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String edit(@RequestBody Map requestMap){
        int id=ParamConvertTool.getParamInt(requestMap,"id");
        String displayTime=ParamConvertTool.getParamStr(requestMap,"displayTime");
        String arrivalTime=ParamConvertTool.getParamStr(requestMap,"arrivalTime");
        int state=ParamConvertTool.getParamInt(requestMap,"state");

        String sql="update OSupply set state=?0,displayTime=?1,arrivalTime=?2 where id=?3";
        if(osupplyService.execHQL(sql,state,displayTime,arrivalTime,id)==1){
            return JsonTool.returnObjectJsonSuccess(null);
        }else{
            return JsonTool.returnObjectJsonError(null);
        }
    }

    @RequestMapping(value = "updateState", method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE,consumes =MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String updateState(@RequestBody Map requestMap){

        String whereIds=ParamConvertTool.getParamStr(requestMap,"ids");

        if(null==requestMap.get("state")){
            return JsonTool.returnObjectJsonError("参数传递错误");
        }

        int state=ParamConvertTool.getParamInt(requestMap,"state");
        if("".equals(whereIds)){
            return JsonTool.returnObjectJsonError("参数传递错误");
        }

        String sql="update OSupply set state=?0 where id in("+whereIds+")";
        if(osupplyService.execHQL(sql,state)==1){
            return JsonTool.returnObjectJsonSuccess(null);
        }else{
            return JsonTool.returnObjectJsonError(null);
        }

    }


}